<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
set_time_limit(0);

class MgamesController extends AppController {

    var $name = 'Mgames';
    var $components = array(
        'Session', 'Alias', 'Upload'
    );
    var $helpers = array(
        'Editor', 'Published', 'Hotgame','Popular'
    );
    var $uses = array(
        'Mgame', 'MgameTechnical'
    );

    function index() {
        $data = $this->data;
        $this->set('title_for_layout', Configure::read('site_name') . ' - Games');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        
        $cat_id = array_key_exists('cat_id', $data) ? $data['cat_id'] : false;
        $technical_id = array_key_exists('technical_id', $data) ? $data['technical_id'] : false;
        $group_id = array_key_exists('group_id', $data) ? $data['group_id'] : false;
        
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'Mgame.title LIKE' => "%$search%",
                'Mgame.id LIKE' => "%$search%",
            );
        }
        if (!empty($cat_id)) {
            $conditions['AND'] = array(
                'Mgame.cat_id' => $cat_id
            );
        }
        if (!empty($technical_id)) {
            $conditions['AND'] = array(
                'Mgame.technical_id' => $technical_id
            );
        }
        if (!empty($group_id)) {
            $conditions['AND'] = array(
                'Mgame.group_id' => $group_id
            );
        }
        
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => 150,
            'order' => array(
                'Mgame.id' => 'DESC'
            ),
        );

        $data = $this->paginate('Mgame');
        $this->set('cat_id', $cat_id);
        $this->set('technical_id', $technical_id);
        $this->set('group_id', $group_id);
        $this->set('game_cat_all', $this->Mgame->Catmgame->getListAll());
        $this->set('group_cat_all', $this->Mgame->MgameGroup->getListAll());
        $this->set('technical_cat_all', $this->Mgame->MgameTechnical->getListAll());
        $this->set('total', $this->params['paging']['Mgame']['count']);
        $this->set('total_inpage', sizeof($data));
        $this->set('data', $data);
        $this->set('keyword', $search);

        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->Mgame->alias}.{$this->Mgame->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );

        //delete flash
        $data = $this->Mgame->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Mgame.id', 'Mgame.thumbnail')
             )
        );

        foreach ($data AS $folder => $filename) {
            //delete it
            if (is_file(Configure::read('mgame_absolute_path') . $filename)) {
                $this->Upload->delete(Configure::read('mgame_absolute_path') . $filename);
            }
        }

        if ($this->Mgame->deleteAll($conditions)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d Game", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }
    function edit() {
        $params = array_merge($this->params['form'], $this->params['named']);

        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Mgame->id = $params['cid'][0];
        } else {
            $this->Mgame->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }

        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $fields = array(
            '*'
        );

        $this->data = $this->Mgame->read($fields);
        $this->set('description', $this->data['Mgame']['description']);
        $check_thumbnail_exist = is_file(Configure::read('mgame_absolute_path') . $this->data['Mgame']['thumbnail']);
        $this->set('check_thumbnail_exist', $check_thumbnail_exist);
        $this->set('image_url', Configure::read('mgame_thumbnail_url_path') . $this->data['Mgame']['thumbnail']);
        $this->set('cat_data', $this->Mgame->Catmgame->getListAll());
        $this->set('group_cat_all', $this->Mgame->MgameGroup->getListAll());
        $this->set('technical_data', $this->Mgame->MgameTechnical->getListAll());
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm game');
        $this->set('cat_data', $this->Mgame->Catmgame->getListAll());
        $this->set('technical_data', $this->Mgame->MgameTechnical->getListAll());
        $this->set('group_cat_all', $this->Mgame->MgameGroup->getListAll());
    }

    function save() {
        $data = $this->data['Mgame'];
        $row = $this->Mgame->findById($this->data['Mgame']['id']);
        
        //for upload game flash
        $this->Upload->setUploadpath(Configure::read('mgame_absolute_path'));
        
        //upload thumbnail
        if ($this->data['Mgame']['thumbnail']['name'] != '') {
            if ($this->Upload->validate($this->data['Mgame']['thumbnail'])) {
                //delete it
                if (is_file( Configure::read('mgame_absolute_path') . $row['Mgame']['thumbnail'])) {
                    $this->Upload->delete(Configure::read('mgame_absolute_path') . $row['Mgame']['thumbnail']);
                }
                $data['thumbnail'] = $this->Upload->upload($this->data['Mgame']['thumbnail'], false);
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'mgames', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['thumbnail']);
        }


        if (!empty($data['id'])) {
            $data['user_modified'] = $this->Auth->user('id');
            $data['user_modified_ip'] = $_SERVER['REMOTE_ADDR'];        
        } else {
            $data['ordering'] = $this->Mgame->nextOrder();
            $data['user_created'] = $this->Auth->user('id');
            $data['user_created_ip'] = $_SERVER['REMOTE_ADDR'];
        }



        $data['alias'] = $this->Alias->alias($data['title']);
        if ($this->Mgame->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Mgame->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'mgames', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'mgames', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Mgame->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Mgame->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'mgames', 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Mgame->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Mgame->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'mgames', 'action' => 'index'));
    }

    function saveorder() {
        $ordering = $this->params['form']['order'];

        foreach ($ordering AS $id => $order) {
            $fields = array(
                'ordering' => $order,
                'id' => $id,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );

            $this->Mgame->saveAll($fields);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('controller' => 'mgames', 'action' => 'index'));
    }
}

